package 贪心算法;
/*
题目描述：一次股票交易包含买入和卖出，只进行一次交易，求最大收益。
 */
public class 股票的最大收益_121 {
	
	public int maxProfit(int[] prices)
	{
		int n = prices.length;
		if (n == 0) return 0;
		int soFarMin = prices[0];
		int max = 0;
		for (int i = 1; i < n; i++) {
			if (soFarMin > prices[i]) soFarMin = prices[i];
			else max = Math.max(max, prices[i] - soFarMin);
		}
		return max;
	}


	public int maxProfit2(int[] prices)
	{
		int n = prices.length;
		if (n == 0) return 0;
		int minPrice = prices[0];
		int max = 0;
		for (int i = 1; i < n; i++) {
			if( prices[i] < minPrice)  minPrice = prices[i];
			else max =Math.max(max,prices[i]-minPrice);
		}
		return max;
	}

}
